<script setup lang="ts">
import * as userAPI from '@/api/user'
import {onMounted, reactive, ref} from "vue";
import {Plus} from '@element-plus/icons-vue'
import router from "@/router";

const pageData = reactive({
  userMessageList: [],
  pageParams: {
    pageSize: 5,
    pageLocal: 1
  },
  total: 10
})

// 分页设置
const pageSettings = reactive({
  pageSize: 5,
  pageLocal: 1
})

const pageDisabled = ref(false)

// 加载用户信息列表
const getUserMessageList = async () => {
  pageDisabled.value = true
  try {
    const data = await userAPI.getUserMessageList(pageSettings)
    pageData.total = data.total
    pageData.userMessageList = data.userMessageList as any
  }catch (e) {
    return
  }finally {
    pageDisabled.value = false
  }
}

onMounted(async () => {
  await getUserMessageList()
})


</script>

<template>
  <div class="main-container">
    <div class="main-header">
      <el-button type="success" @click="async () => await router.replace('/user/register')">
        <template #icon>
          <el-icon><Plus/></el-icon>
        </template>
      </el-button>
    </div>
    <div class="main-main">
      <el-table :data="pageData.userMessageList" stripe border>
        <el-table-column type="expand">
          <template #default="scope">
            <div class="introduction">
              <h3>自我介绍</h3>
              <p v-text="scope.row.userSelfIntroduction"></p>
            </div>
          </template>
        </el-table-column>
        <el-table-column type="index" label="索引" min-width="30px"/>
        <el-table-column label="姓名" prop="userName"/>
        <el-table-column label="性别" prop="userSex"/>
        <el-table-column label="年龄" prop="userAge"/>
        <el-table-column label="身份证号" prop="userIdCard" min-width="120px"/>
        <el-table-column label="电话号码" prop="userPhone"/>
        <el-table-column label="收入" prop="userIncome"/>
        <el-table-column label="地址" prop="userAddress"/>
      </el-table>
    </div>
    <div class="main-footer">
      <el-pagination
          v-model:current-page="pageData.pageParams.pageLocal"
          v-model:page-size="pageData.pageParams.pageSize"
          :page-sizes="[1, 2, 3, 4, 5]"
          :total="pageData.total"
          :pager-count="5"
          :disabled="pageDisabled"
          background
          layout="total, sizes, prev, pager, next, jumper"
          @update:current-page="getUserMessageList"
          @update:page-size="getUserMessageList"
      />
    </div>
  </div>
</template>

<style scoped lang="less">
  .main-container {
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    min-height: 100%;
    padding: 20px 20px 0;
  }

  .main-header {
    height: 60px;
  }

  .main-main {
    position: relative;
    flex: 1;
    height: 0;

    .introduction {
      h3 {
        font-size: 20px;
      }

      p {
        text-indent: 2em;
        font-size: 18px;
      }
    }
  }

  .main-footer {
    margin-top: 20px;
    margin-bottom: 10px;
    .el-pagination {
      justify-content: center;
      height: 100%;
    }
  }
</style>